package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.DamageListDao;
import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.service.DamageListService;
import com.atguigu.jxc.util.DateUtil;
import com.atguigu.jxc.util.StringUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Type;
import java.util.*;

/**
 * @author ：Shine
 * @description：
 * @date ：2021/3/23 17:42
 */
@Service
public class DamageListServiceImpl implements DamageListService {
    @Autowired
    private DamageListGoodsDao damageListGoodsDao;

    @Autowired
    private DamageListDao damageListDao;

    @Override
    public ServiceVO save(DamageList damageList, String damageListGoodsStr) {
        //保存报损单
        if (damageList == null || StringUtil.isEmpty(damageListGoodsStr)) {
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
        damageListDao.insert(damageList);

        Gson gson = new Gson();
        Type type = new TypeToken<ArrayList<DamageListGoods>>() {
        }.getType();
        List<DamageListGoods> damageListGoodsList = gson.fromJson(damageListGoodsStr, type);
        if (CollectionUtils.isEmpty(damageListGoodsList)) {
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }

        for (DamageListGoods damageListGoods : damageListGoodsList) {
            damageListGoods.setDamageListId(damageList.getDamageListId());
            damageListGoodsDao.insert(damageListGoods);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> list(String sTime, String eTime) {
        //    报损单查询
        Map<String, Object> map = new HashMap<>();
        List<DamageList> damageLists = damageListDao.selectList(sTime, eTime);
        map.put("rows", damageLists);
        return map;
    }

    /**
     * 查询报损单商品信息
     *
     * @param damageListId
     * @return
     */
    @Override
    public Map<String, Object> goodsList(Integer damageListId) {
        Map<String, Object> map = new HashMap<>();
        List<DamageListGoods> damageListGoods = damageListGoodsDao.selectList(damageListId);
        map.put("rows", damageListGoods);
        return map;
    }
}
